const getUpDownDays = require('./utils/getUpDownDays')
const reqlib = require('app-root-path').require
const btbAs = reqlib('/app/common/btb')

function AnalysisInfoFlag (config) {
  this.type = config.type || '15m'
  this.code = config.code
  // 从近到远
  this.analysisInfoList = config.analysisInfoList || []
  this.nowEmaInfo = this.analysisInfoList[0] || {}
  this.lastEmaInfo = this.analysisInfoList[1] || {}
  this.upDownDaysInfo = getUpDownDays(this.analysisInfoList)
  // 包括万象了
  this.flagInfo = this.getAllFlag()
}

AnalysisInfoFlag.prototype = {
  getAllFlag () {
    return {
      ...this.nowEmaInfo,
      ...this.upDownDaysInfo,
      analysisInfoList: this.analysisInfoList,
      netChangeRatioList: this.analysisInfoList.map((v) => {
        return v.netChangeRatio || 0
      }),
      // 实际是34
      hasFullData: this.analysisInfoList.length >= 40,
      hasFullDataLarge: this.analysisInfoList.length >= 80,
      isNowRiseHighAndFall: this.isNowRiseHighAndFall(),
      isRecentNeedleLine: this.isRecentNeedleLine(),
      before10DaysAvMaxWave: this.getBeforeRecentDaysAvMaxWave(10),
      before10DaysAvRealWave: this.getBeforeRecentDaysAvRealWave(10),
      before30DaysNeedleLineTimes: this.getBeforeRecentDaysNeedleLineTimes(30),
      is20DaysNewHigh: this.getIsBeforeRecentDaysNewHigh(20),
      is20DaysNewLow: this.getIsBeforeRecentDaysNewLow(20),
      highLowInfo20: this.getBeforeInfoRecentDaysNewHighLow(20),
      is40DaysNewHigh: this.getIsBeforeRecentDaysNewHigh(40),
      is40DaysNewLow: this.getIsBeforeRecentDaysNewLow(40),
      highLowInfo40: this.getBeforeInfoRecentDaysNewHighLow(40),
      // ema往下穿梭
      isEmaDownShuttle: this.getIsEmaDownShuttle(),
      // ema往上穿梭
      isEmaUpShuttle: this.getIsEmaUpShuttle(),
      netChangeRatio2Days: btbAs.getDaysNetChangeRatio(this.analysisInfoList, 2)
    }
  },
  // ema往下穿梭
  getIsEmaDownShuttle () {
    if (
      this.upDownDaysInfo.ema5B10Days >= 1 &&
      (this.upDownDaysInfo.ema5W10B20Days <= 1)
    ) {
      return true
    }
    return false
  },
  // ema往上穿梭
  getIsEmaUpShuttle () {
    if (
      this.upDownDaysInfo.ema5A10Days >= 1 &&
      (this.upDownDaysInfo.ema5W10A20Days <= 1)
    ) {
      return true
    }
    return false
  },
  getNetChangeRatioUpDaysInRange (rangeDays) {
    return btbAs.getNetChangeRatioUpDaysInRange(this.analysisInfoList, rangeDays)
  },
  getNetChangeRatioDownDaysInRange (rangeDays) {
    return btbAs.getNetChangeRatioDownDaysInRange(this.analysisInfoList, rangeDays)
  },
  getMacdUpDaysInRange (rangeDays) {
    return btbAs.getMacdUpDaysInRange(this.analysisInfoList, rangeDays)
  },
  getMacdDownDaysInRange (rangeDays) {
    return btbAs.getMacdDownDaysInRange(this.analysisInfoList, rangeDays)
  },
  // 冲搞回落
  isNowRiseHighAndFall () {
    return btbAs.isNowRiseHighAndFall(this.analysisInfoList)
  },
  // 近期很多针线
  isRecentNeedleLine (days) {
    return btbAs.isRecentNeedleLine(this.analysisInfoList, days)
  },
  // 近短时间，金针天数
  getBeforeRecentDaysNeedleLineTimes (days) {
    return btbAs.getBeforeRecentDaysNeedleLineTimes(this.analysisInfoList, days)
  },
  getBeforeRecentDaysAvMaxWave (days) {
    return btbAs.getBeforeRecentDaysAvMaxWave(this.analysisInfoList, days)
  },
  getBeforeRecentDaysAvRealWave (days) {
    return btbAs.getBeforeRecentDaysAvRealWave(this.analysisInfoList, days)
  },
  getRecentDaysMaxRealWave (days) {
    return btbAs.getRecentDaysMaxRealWave(this.analysisInfoList, days)
  },
  getIsBeforeRecentDaysNewHigh (days) {
    return btbAs.getIsBeforeRecentDaysNewHigh(this.analysisInfoList, days)
  },
  getIsBeforeRecentDaysNewLow (days) {
    return btbAs.getIsBeforeRecentDaysNewLow(this.analysisInfoList, days)
  },
  // 统计一段时间内的次数
  countIfMacdUpTimes (days, times) {
    return btbAs.countIfMacdUpTimes(this.analysisInfoList, days, times)
  },
  countIfMacdDownTimes (days, times) {
    return btbAs.countIfMacdDownTimes(this.analysisInfoList, days, times)
  },
  countIf5W10A20Times (days, times) {
    return btbAs.countIf5W10A20Times(this.analysisInfoList, days, times)
  },
  countIf5W10B20Times (days, times) {
    return btbAs.countIf5W10B20Times(this.analysisInfoList, days, times)
  },
  countLowNetChangeTimes (days) {
    return btbAs.countLowNetChangeTimes(this.analysisInfoList, days)
  },
  countKdjDownTimes (days) {
    return btbAs.countKdjDownTimes(this.analysisInfoList, days)
  },
  // 去掉涨跌幅最大1/4计算剩下的波动能占总波动的多少
  countRemoveMaxResMaxWaveRate (days, times) {
  },
  getBeforeInfoRecentDaysNewHighLow (days) {
    return btbAs.getBeforeInfoRecentDaysNewHighLow(this.analysisInfoList, days)
  },
  // 获取大跌的index
  getRecentDaysHasSuddenlyBigDownIndex (days) {
    for (let i = 0; i < days; i++) {
      const item = this.analysisInfoList[i]
      if (item && item.netChangeRatio <= -25) {
        return i
      }
    }
    return -1
  },
  getRecentDaysHasBigUpDay (days, rate) {
    rate = rate || 20
    for (let i = 0; i < days; i++) {
      const item = this.analysisInfoList[i]
      if (item && item.netChangeRatio > rate) {
        return true
      }
    }
    return false
  },
  getTradeMainInfo () {
    return {
      close: this.nowEmaInfo.close,
      open: this.nowEmaInfo.open,
      low: this.nowEmaInfo.low,
      high: this.nowEmaInfo.high,
      macd: this.nowEmaInfo.macd,
      kdjDiff: this.nowEmaInfo.kdjDiff,
      ema5: this.nowEmaInfo.ema5,
      ema10: this.nowEmaInfo.ema10,
      ema20: this.nowEmaInfo.ema20
    }
  }
}

module.exports = AnalysisInfoFlag
